﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.UC
{
    public partial class UcEditReference : UserControl
    {
        private bool onBinding;
        private ArtefactList _artefactList = null;
        private Artefact ar = null;

        #region EventsHandler

        public event EventHandler ReferenceSelect;

        public void RaiseReferenceSelect()
        {
            EventHandler eventHandler = this.ReferenceSelect;
            if (eventHandler != null)
                this.ReferenceSelect(this, EventArgs.Empty);
        }
        #endregion //EventsHandler

        public UcEditReference()
        {
            InitializeComponent();
        }

        public string LabelRef
        {
            set
            {
                this.labelrefId.Text = value;
            }
        

        }
        private ArtefactRefBase _artefactRef;
        public ArtefactRefBase ArtefactRef
        {
            get 
            { 
                return _artefactRef; 
            }
            set 
            { 
                _artefactRef = value;
                BindRef();
            }
        }

        private SdmxArtefactType _artefactType;
        public SdmxArtefactType AtrefactType
        {
            get { return _artefactType; }
            set
            {
                _artefactType = value;
            }
        }

        private void BindRef()
        {
            onBinding = true;
            if (_artefactRef != null && _artefactType != SdmxArtefactType.None)
            {
                _artefactList = ReferenceManager.GetArtefactList(_artefactType);
                ar = ReferenceManager.LocateArtefact(_artefactList, _artefactRef);
                string name = ar == null ? string.Empty : ar.Name.ToString();
                if (_artefactRef.Id != string.Empty && ar == null)
                    buttonCodelistRefError.Visible = true;
                else
                    buttonCodelistRefError.Visible = false;

                multiColumnComboBoxCodelist.DataSource = _artefactList.GetTableRepresentation();
                multiColumnComboBoxCodelist.ColumnWidths = multiColumnComboBoxCodelist.Width.ToString() + ";" + (textBoxCodelistName.Width + 20).ToString() + ":" + textBoxAgency.Width.ToString();
                if (ar != null)
                    multiColumnComboBoxCodelist.Text = ar.Id;
                else
                    multiColumnComboBoxCodelist.Text = _artefactRef.Id;

                if (string.IsNullOrEmpty(_artefactRef.Id))
                {
                    textBoxCodelistName.Text = string.Empty;
                    textBoxAgency.Text = string.Empty;
                    multiColumnComboBoxCodelist.Text = null;
                }
                else
                {
                    multiColumnComboBoxCodelist.Text = _artefactRef.Id;
                    textBoxCodelistName.Text = _artefactRef.Version + " - " + name;
                    textBoxAgency.Text = _artefactRef.AgencyId;
                }
            }

            onBinding = false;
        }

        private void multiColumnComboBoxCodelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (onBinding)
                return;

            DataTable dt = multiColumnComboBoxCodelist.DataSource as DataTable;
            if (dt != null && multiColumnComboBoxCodelist.SelectedIndex >= 0)
            {
                ar = _artefactList[multiColumnComboBoxCodelist.SelectedIndex] as Artefact;
                if (_artefactRef != null)
                {
                    _artefactRef.Id = ar.Id;
                    _artefactRef.Version = ar.Version;
                    _artefactRef.AgencyId = ar.AgencyId;
                    textBoxAgency.Text = dt.Rows[multiColumnComboBoxCodelist.SelectedIndex][2].ToString();
                }
            }
            else
            {
                textBoxCodelistName.Text = string.Empty;
                textBoxAgency.Text = string.Empty;
            }
            buttonCodelistRefError.Visible = ar == null;
            RaiseReferenceSelect();
        }

        private void buttonNavigate_Click(object sender, EventArgs e)
        {
            if (ArtefactRef.IsFilled)
            {
                try
                {
                    ArtefactRefTyped art = new ArtefactRefTyped(AtrefactType, ArtefactRef.FullIdent);

                    // If already in memory, navigate otherwhile get from registry
                    if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, null) == null)
                        NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, null);
                }
                catch { }
            }
        }

        #region Urn Paste

        private void buttonAddUrl_Click(object sender, EventArgs e)
        {
            try
            {
                ArtefactRefTyped art = ReferenceManager.ArTypedCopyInstance as ArtefactRefTyped;
                if (art != null && art.Type == AtrefactType)
                {
                    UrnParser p = new UrnParser(ReferenceManager.ArTypedCopyInstanceUrn);
                    if (p.IsSupported)
                    {
                        ArtefactRefBase arb = new ArtefactRefBase(p.ArtefactId, p.AgencyId, p.Version);
                        ArtefactRef = arb;
                        RaiseReferenceSelect();
                    }
                }
            }
            catch { }
        }
        #endregion

    }
}
